package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailBaseInfoService;
import com.xbongbong.crm.handle.analysis.CustomerCommunicateDetailAnalysisDataHandler;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.explain.FormDataDetailFormExplainHandleName;
import com.xbongbong.parent.parse.data.FormDataGetAnalysisDataHelp;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.model.CustomerCommunicateModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;

/**
 * @author sunshaoxiang
 * @ClassName CustomerCommunicateDetailBaseInfoServiceImpl
 * @Description 跟进记录详情页 base info 业务层
 * @Date 2022/6/7 14:47
 * @Version 1.0
 **/
@Service("customerCommunicateDetailBaseInfoServiceImpl")
public class CustomerCommunicateDetailBaseInfoServiceImpl extends AbstractCrmDetailBaseInfoService {

    public static final Logger LOGGER = LoggerFactory.getLogger(CustomerCommunicateDetailBaseInfoServiceImpl.class);

    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailFormExplainHandleName formDataDetailFormExplainHandleName;
    @Resource
    private CustomerCommunicateDetailAnalysisDataHandler customerCommunicateDetailAnalysisDataHandler;
    @Resource
    private CustomerCommunicateModel customerCommunicateModel;
    @Resource
    private FormDataGetAnalysisDataHelp formDataGetAnalysisDataHelp;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {

    }

    @Override
    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        handlerExplainDTO.setExplainList(explainList);
        ownerAndCoUserHelp.getOwnerIdAndCoUserId(handlerExplainDTO, handlerExplainDTO.getSaasMark(), handlerExplainDTO.getBusinessType(), paasFormDataEntityExt, new HashMap<>(), DelEnum.NORMAL.getDel());
        HandlerExplainVO handlerExplainVO = formDataDetailFormExplainHandleName.handleExplain(explainList, formEntity, paasFormDataEntityExt, handlerExplainDTO);
        explainList = handlerExplainVO.getHeadList();
        //跟进记录特殊业务解释处理
        handleDetailSpecialExplain(paasFormDataEntityExt.getData(), explainList, handlerExplainDTO.getPlatform());
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
        //
        formatExplainDTO.setExplainList(explainList);
        formatExplainDTO.setResult(paasFormDataEntityExt.getData());
        customerCommunicateModel.formatUpdateExplain(formatExplainDTO);
        return handlerExplainVO;
    }

    /**
     * 跟进记录特殊业务解释处理
     * 重构前 -> com.xbongbong.saas.help.ProDetailHandlerHelp#handleDetailSpecialExplain(com.alibaba.fastjson.JSONObject, java.util.List, java.lang.Integer, java.lang.String, java.lang.String, java.lang.Long)
     * @param data
     * @param explainList
     * @param platForm
     * @throws XbbException
     */
    private void handleDetailSpecialExplain(JSONObject data, List<FieldAttrEntity> explainList, String platForm) throws XbbException {
        Integer ifRemind = getIntegerOrDefaultFromFormData(data, CustomerCommunicateEnum.IF_REMIND.getAttr(), BasicConstant.ZERO);
        if (ifRemind == 1) {
            if (Objects.equals(PlatFormEnum.DINGTALK.getValue(), platForm)) {
                // 移动端跟进记录详情基本信息不展示图片和录音
                explainList.removeIf(fieldAttrEntity -> Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.VIDEO.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.IMAGES.getAttr()));
            } else {
                explainList.removeIf(fieldAttrEntity -> Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.IMAGES.getAttr()));
            }
        } else {
            if (Objects.equals(PlatFormEnum.DINGTALK.getValue(), platForm)) {
                // 移动端跟进记录详情基本信息不展示图片和录音
                explainList.removeIf(fieldAttrEntity -> Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.VIDEO.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.IMAGES.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NEXT_COMMUNICATE_TIME.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NOTIFY_TYPE.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NOTIFY_TIME.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NEXT_NOTIFY_MEMO.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.ENTER_COMMUNICATE_PLAN.getAttr()));
            } else {
                explainList.removeIf(fieldAttrEntity -> Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.IMAGES.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NEXT_COMMUNICATE_TIME.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NOTIFY_TYPE.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NOTIFY_TIME.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.NEXT_NOTIFY_MEMO.getAttr()) ||
                        Objects.equals(fieldAttrEntity.getAttr(), CustomerCommunicateEnum.ENTER_COMMUNICATE_PLAN.getAttr()));
            }
        }
    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        String corpid = handlerExplainDTO.getCorpid();
        Integer businessType = handlerExplainDTO.getBusinessType();
        Long formId = paasFormDataEntityExt.getFormId();
        List<FieldAttrEntity> headList = handlerExplainVO.getHeadList();
        String platform = handlerExplainDTO.getPlatform();
        Long dataId = paasFormDataEntityExt.getId();
        list.forEach(item->explainMap.put(item.getAttr(), item));
        JSONObject data = commonAnalysisAllData(paasFormDataEntityExt, handlerExplainDTO, handlerExplainVO, list, explainMap);

        customerCommunicateDetailAnalysisDataHandler.handleDetailSpecialField(data, dataId, businessType, corpid, explainMap, formId, handlerExplainDTO);

        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        saasAnalysisDataDTO.setFormId(formId);
        customerCommunicateModel.formatData(saasAnalysisDataDTO, data);
        return data;

    }
}
